package com.htgd.gardenexpo.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dao.GeMyFeedbackUploadDao;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConFood;
import com.htgd.gardenexpo.entity.GeMyFeedback;
import com.htgd.gardenexpo.dao.GeMyFeedbackDao;
import com.htgd.gardenexpo.entity.GeMyFeedbackUpload;
import com.htgd.gardenexpo.page.FeedbackQuery;
import com.htgd.gardenexpo.page.FoodQuery;
import com.htgd.gardenexpo.service.GeBaseDictItemService;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.GeMyFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.FeedbackList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 反馈表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-09
 */
@Service
public class GeMyFeedbackServiceImpl extends ServiceImpl<GeMyFeedbackDao, GeMyFeedback> implements GeMyFeedbackService {

    @Autowired
    private GeMyFeedbackDao geMyFeedbackDao;

    @Autowired
    private GeMyFeedbackUploadDao geMyFeedbackUploadDao;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @Autowired
    private GeBaseDictItemService geBaseDictItemService;

    @Override
    public PageInfo<GeMyFeedback> queryList(FeedbackQuery feedbackQuery) {
        if(feedbackQuery.getType()!=null&&feedbackQuery.getType()!=""){
            feedbackQuery.setType(geBaseDictService.getFeedbackTypeText(feedbackQuery.getType()));
        }
        PageHelper.startPage(feedbackQuery.getPageNum(), feedbackQuery.getPageSize());
        List<GeMyFeedback> list = geMyFeedbackDao.querylist(feedbackQuery);
        for (GeMyFeedback geMyFeedback : list) {
            geMyFeedback.setOpenId("");
            QueryWrapper<GeMyFeedbackUpload> geMyFeedbackUploadQueryWrapper = new QueryWrapper<>();
            List<GeMyFeedbackUpload> geMyFeedbackUploads = geMyFeedbackUploadDao.selectList(geMyFeedbackUploadQueryWrapper.eq("feedback_id", geMyFeedback.getId()));
            String dictId = geBaseDictService.getByCode("feedback_type");
            geMyFeedback.setTypeText(geMyFeedback.getType());
            if(dictId!=null&&dictId!=""){
                geMyFeedback.setType(geBaseDictItemService.getItemByCodeAndText(dictId,geMyFeedback.getType()));
            }else{
                geMyFeedback.setType(null);
            }
            if(CollUtil.isNotEmpty(geMyFeedbackUploads)){
                geMyFeedback.setUploadList(geMyFeedbackUploads);
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public GeMyFeedback details(Integer id) {
        GeMyFeedback geMyFeedback = geMyFeedbackDao.selectById(id);
        geMyFeedback.setOpenId("");
        String dictId = geBaseDictService.getByCode("feedback_type");
        if(dictId!=null&&dictId!=""){
            geMyFeedback.setType(geBaseDictItemService.getItemByCodeAndText(dictId,geMyFeedback.getType()));
        }else{
            geMyFeedback.setType(null);
        }
        geMyFeedback.setTypeText(geMyFeedback.getType());
        QueryWrapper<GeMyFeedbackUpload> geMyFeedbackUploadQueryWrapper = new QueryWrapper<>();
        List<GeMyFeedbackUpload> geMyFeedbackUploads = geMyFeedbackUploadDao.selectList(geMyFeedbackUploadQueryWrapper.eq("feedback_id", geMyFeedback.getId()));
        if(CollUtil.isNotEmpty(geMyFeedbackUploads)){
            geMyFeedback.setUploadList(geMyFeedbackUploads);
        }

        return geMyFeedback;
    }

    @Override
    public PageInfo<GeMyFeedback> getFeedbackByOpenId(String openId, FeedbackQuery feedbackQuery) {
        PageHelper.startPage(feedbackQuery.getPageNum(),feedbackQuery.getPageSize());
        String type = "";
        if(feedbackQuery.getType()!=null&&feedbackQuery.getType()!=""){
            if(Objects.equals(feedbackQuery.getType(),"0")){
                type = "Zheg线上寻物";
            }
            if(Objects.equals(feedbackQuery.getType(),"1")){
                type = "咨询建议";
            }
            if(Objects.equals(feedbackQuery.getType(),"2")){
                type = "投诉举报";
            }
            //geBaseDictService.getFeedbackTypeText(feedbackQuery.getType());
        }
        if(type==""){
            return new PageInfo<>(new ArrayList<>());
        }
        List<GeMyFeedback> list = geMyFeedbackDao.getFeedbackByOpenId(openId,type);
        for (GeMyFeedback geMyFeedback : list) {
            if(geMyFeedback.getReply()!=null){
                geMyFeedback.setIsReply(true);
            }else {
                geMyFeedback.setIsReply(false);
            }
            String dictId = geBaseDictService.getByCode("feedback_type");
            if(dictId!=null&&dictId!=""){
                geMyFeedback.setType(geBaseDictItemService.getItemByCodeAndText(dictId,geMyFeedback.getType()));
            }else{
                geMyFeedback.setType(null);
            }
            geMyFeedback.setSecondType(geMyFeedback.getSecondType());
            geMyFeedback.setTypeText(geMyFeedback.getType());
            geMyFeedback.setContent(geMyFeedback.getContent());
            geMyFeedback.setId(geMyFeedback.getId());
        }
        return new PageInfo<>(list);
    }

    @Override
    public Long getCount() {
        return lambdaQuery().count();
    }

    @Override
    public CommonResult getConsultationAndSuggestionNums() {
        Long nums = geMyFeedbackDao.getConsultationAndSuggestionNums();
        return CommonResult.success("查询成功",nums);
    }

}
